package com.pengtu.utils.jbpm.rollback;

import org.jbpm.api.TaskService;
import org.jbpm.api.cmd.Command;
import org.jbpm.api.cmd.Environment;
import org.jbpm.api.task.Participation;
import org.jbpm.api.task.Task;

public class TaskDelegateCmd implements Command<Void> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String taskId;
	private String delegateUserId;
	
	public TaskDelegateCmd(String taskId,String delegateUserId){
		this.taskId = taskId;
		this.delegateUserId = delegateUserId;
	}

	@Override
	public Void execute(Environment environment) throws Exception {
		// TODO Auto-generated method stub
		TaskService taskService = environment.get(TaskService.class);
		Task task = taskService.getTask(taskId);
		taskService.addTaskParticipatingUser(taskId, task.getAssignee(), Participation.REPLACED_ASSIGNEE);
		taskService.addTaskComment(taskId, "Delegate task from " + task.getAssignee() + " to " + delegateUserId);
		task.setAssignee(delegateUserId);
		taskService.saveTask(task);
		return null;
	}

}
